package com.lry.crawlers.fiction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.fiction.entity.Fiction;
import com.lry.crawlers.fiction.mapper.FictionMapper;
import com.lry.crawlers.fiction.service.FictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FictionServiceImpl implements FictionService {

    @Autowired
    FictionMapper fictionMapper;

    @Override
    public int setFiction(Fiction fiction) {
        int insert = fictionMapper.insert(fiction);
        return insert;
    }


    @Override
    public int setFictions(List<Fiction> fictions) {
        int i = fictionMapper.insertBatchSomeColumn(fictions);
        return i;
    }


    @Override
    public List<Integer> removeDuplicates(List<String> titles) {
        List<Integer> duplicates = new ArrayList<>();
        for(int i = 0;i<titles.size();i++){
            QueryWrapper wrapper  = new QueryWrapper();
            wrapper.eq("title",titles.get(i));
            Fiction f =fictionMapper.selectOne(wrapper);
            if(f!=null){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }

    @Override
    public void setupdateTime(Fiction fiction)  {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("fiction_id",fiction.getFictionId());
        fictionMapper.update(fiction,wrapper);
    }

    @Override
    public boolean removeDuplicate(String s) {
        QueryWrapper wrapper  = new QueryWrapper();
        wrapper.eq("title",s);
        Fiction f =fictionMapper.selectOne(wrapper);
        return f==null? false :true;
    }

}


